package com.ruoyi.web.controller.work;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.work.domain.WorkExpense;
import com.ruoyi.work.service.IWorkExpenseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报销登记Controller
 *
 * @author ruoyi
 * @date 2022-02-03
 */
@Controller
@RequestMapping("/work/expense")
public class WorkExpenseController extends BaseController {
    private String prefix = "work/expense";

    @Autowired
    private IWorkExpenseService workExpenseService;

    @RequiresPermissions("work:expense:view")
    @GetMapping()
    public String expense() {
        return prefix + "/expense";
    }

    /**
     * 查询报销登记列表
     */
    @RequiresPermissions("work:expense:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WorkExpense workExpense) {
        startPage();
        List<WorkExpense> list = workExpenseService.selectWorkExpenseList(workExpense);
        return getDataTable(list);
    }

    /**
     * 销账查询报销登记列表
     */
/*    @RequiresPermissions("work:expense:cancel")*/
    @PostMapping("/cancel/{exType}")
    @ResponseBody
    public TableDataInfo cancel(@PathVariable("exType") String exType) {
        startPage();
        WorkExpense workExpense = new WorkExpense();
        workExpense.setExpenseType(exType);
        workExpense.setCreateBy(getLoginName());
        List<WorkExpense> list = workExpenseService.selectWorkExpenseList(workExpense);
        return getDataTable(list);
    }

    /**
     * 导出报销登记列表
     */
    @RequiresPermissions("work:expense:export")
    @Log(title = "报销登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WorkExpense workExpense) {
        List<WorkExpense> list = workExpenseService.selectWorkExpenseList(workExpense);
        ExcelUtil<WorkExpense> util = new ExcelUtil<WorkExpense>(WorkExpense.class);
        return util.exportExcel(list, "报销登记数据");
    }

    /**
     * 新增报销登记
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存报销登记
     */
    @RequiresPermissions("work:expense:add")
    @Log(title = "报销登记", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkExpense workExpense) {
        workExpense.setExpenser(getUserName());
        return toAjax(workExpenseService.insertWorkExpense(workExpense));
    }

    /**
     * 修改报销登记
     */
    @RequiresPermissions("work:expense:edit")
    @GetMapping("/edit/{expenseId}")
    public String edit(@PathVariable("expenseId") Long expenseId, ModelMap mmap) {
        WorkExpense workExpense = workExpenseService.selectWorkExpenseByExpenseId(expenseId);
        mmap.put("workExpense", workExpense);
        return prefix + "/edit";
    }

    /**
     * 修改保存报销登记
     */
    @RequiresPermissions("work:expense:edit")
    @Log(title = "报销登记", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkExpense workExpense) {
        return toAjax(workExpenseService.updateWorkExpense(workExpense));
    }

    /**
     * 删除报销登记
     */
    @RequiresPermissions("work:expense:remove")
    @Log(title = "报销登记", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(workExpenseService.deleteWorkExpenseByExpenseIds(ids));
    }

    /**
     * 查询销账税金
     */
/*    @RequiresPermissions("work:expense:tax")*/
    @PostMapping("/tax/{incomingId}")
    @ResponseBody
    public TableDataInfo tax(@PathVariable("incomingId") Long incomingId) {
        startPage();
        List<WorkExpense> list = workExpenseService.selectTaxByCancel(incomingId);
        return getDataTable(list);
    }

    /**
     * 查询销账税金
     */
/*    @RequiresPermissions("work:expense:backpay")*/
    @PostMapping("/backpay/{incomingId}")
    @ResponseBody
    public TableDataInfo backpay(@PathVariable("incomingId") Long incomingId) {
        startPage();
        List<WorkExpense> list = workExpenseService.selectBackByCancel(incomingId);
        return getDataTable(list);
    }

    /**
     * 审核报销登记
     */
    @RequiresPermissions("work:expense:check")
    @Log(title = "报销登记", businessType = BusinessType.CHECK)
    @PostMapping("/check")
    @ResponseBody
    public AjaxResult check(String[] ids) {
        try {
            workExpenseService.selectWorkExpenseByExpenseIds(ids).forEach(e -> {

                e.setExpenseStatus("2");
                workExpenseService.updateWorkExpense(e);
            });
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

}
